package com.araim.rringtone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

public class NewAlarmChanger extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		Log.d(RandomRingtone.TAG, "New Alarm or alarm enable/disable detected");
		SharedPreferences enabledSettigns = context.getSharedPreferences(RandomRingtone.ENABLED_ALARMS, 0);
		boolean defaultAlarm = enabledSettigns.getBoolean("alarm-1", false);
		if (defaultAlarm) {
			Log.d(RandomRingtone.TAG, "Randomizing alarms for all.");
			Alarm.randomizeAlarm(context, -1);
		} else {
			for (Alarm a : Alarm.getAllAlarms(context)) {
				boolean thisAlarm = enabledSettigns.getBoolean("alarm" + a.getId(), false);
				if (thisAlarm) {
					Log.d(RandomRingtone.TAG, "Randomizing alarms for " + a.getId());
					Alarm.randomizeAlarm(context, a.getId());
				}
			}
		}
	}
}
